package com.zerui.manager.mq;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zerui.manager.common.mq.consumer.IotAccessRecordMessage;
import com.zerui.manager.web.AccessRecordService;
import com.zerui.manager.web.cmd.AccessRecordAddCmd;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;

/**
 * 设备通行记录MQ
 *
 * @Author wh
 * @create_time 2023-05-11 17:10
 */
@Slf4j
@Component
@RocketMQMessageListener(consumerGroup = "${rocketmq.consumer.group}", topic = "iot-access-record")
public class DevicePassRecordRocketMqConsumer implements RocketMQListener<IotAccessRecordMessage> {
    @Resource
    private AccessRecordService accessRecordService;
    @Override
    public void onMessage(IotAccessRecordMessage message) {

        log.info("iot access record message receive start message:{}",message);

        String deviceId = message.getDeviceId();

        if(StringUtils.isBlank(deviceId)){
            return;
        }
        IotAccessRecordMessage.Record data = message.getData();
        if(ObjectUtils.isEmpty(data)){
            return;
        }

        AccessRecordAddCmd accessRecordAddCmd = AccessRecordAddCmd.builder()

                .deviceId(deviceId)
                .deviceName(message.getDeviceName())
                .personnelId(data.getPersonId())
                .personnelName(data.getPersonName())
                .mode(data.getRecordType())
                .passStatus(data.getPassResult())
                .recordId(data.getRecordId())
                .time(message.getTime())
                .build();

        accessRecordService.addAccessRecord(accessRecordAddCmd);

    }

}
